package at.fhooe.mcm.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

import at.fhooe.mcm.logic.core.Core;

import at.fhooe.mcm.logic.event.EventManager;

import at.fhooe.mcm.logic.sensors.entities.AirpressureSensor;
import at.fhooe.mcm.logic.sensors.entities.HumiditySensor;
import at.fhooe.mcm.logic.sensors.entities.TemperatureSensor;
import at.fhooe.mcm.logic.sensors.events.SensorAttachEvent;
import at.fhooe.mcm.logic.sensors.events.SensorChangedEvent;
import at.fhooe.mcm.logic.sensors.events.SensorResponseEvent;

public class EditView {

	private JPanel m_periodPanel;
	private JTextField m_periodField;
	private JRadioButton m_privateRadioButton;
	private JRadioButton m_publicRadioButton;
	private JRadioButton m_pollRadioButton;
	private JRadioButton m_pushRadioButton;
	private JComboBox m_typeBox;
	private ButtonGroup m_publicPrivateGroup = new ButtonGroup();
	private ButtonGroup m_pushPollGroup = new ButtonGroup();
	private JTextField m_nameField;
	private JFrame frame;
	private Core m_core;
	private String m_mode = "";
	private String m_id = "";

	/**
	 * Create the application
	 */
	public EditView(Core _c) {
		m_core = _c;
		m_mode = "create";
		createContents();
		frame.setVisible(true);
	}
	
	public EditView(Core _c, SensorResponseEvent _e) {
		m_core = _c;
		m_mode = "edit";
		createContents();
		m_id = _e.getSensorID();
		

		setData(_e);
		
		frame.setVisible(true);
	}
	
	private void setData(SensorResponseEvent _e)
	{
		m_nameField.setText(_e.getSensorName());
		m_typeBox.setEnabled(false);
		if(!_e.isPull())
		{
			m_pushRadioButton.setSelected(true);
			m_pollRadioButton.setSelected(false);
			
		}
		else
		{
			m_pushRadioButton.setSelected(false);
			m_pollRadioButton.setSelected(true);
			m_periodField.setEnabled(false);
		}
		if(_e.isPublic())
		{
			m_publicRadioButton.setSelected(true);
			m_privateRadioButton.setSelected(false);
		}
		else
		{
			m_publicRadioButton.setSelected(false);
			m_privateRadioButton.setSelected(true);
		}
		m_periodField.setText(""+_e.getPushPeriod());
		
		
		
		if(_e.getSensorType().equals(TemperatureSensor.TYPE))
		{
			m_typeBox.setSelectedIndex(0);
		}
		else if(_e.getSensorType().equals(HumiditySensor.TYPE))
		{
			m_typeBox.setSelectedIndex(1);
		}
		else if(_e.getSensorType().equals(AirpressureSensor.TYPE))
		{
			m_typeBox.setSelectedIndex(2);
		}
	}

	/**
	 * Initialize the contents of the frame
	 */
	private void createContents() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setTitle("Sensor");
		frame.getContentPane().setLayout(null);
		frame.setBounds(100, 100, 258, 382);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		final JLabel nameLabel = new JLabel();
		nameLabel.setText("Name");
		nameLabel.setBounds(10, 10, 66, 16);
		frame.getContentPane().add(nameLabel);

		m_nameField = new JTextField();
		m_nameField.setBounds(82, 8, 146, 20);
		frame.getContentPane().add(m_nameField);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null), "Mode", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel.setBounds(12, 138, 218, 67);
		frame.getContentPane().add(panel);

		m_pushRadioButton = new JRadioButton();
		m_pushRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) 
			{
				m_periodPanel.setEnabled(true);
				m_periodField.setEnabled(true);
				System.out.println("enabled");
			}
		});
		m_pushRadioButton.setSelected(true);
		m_pushPollGroup.add(m_pushRadioButton);
		m_pushRadioButton.setBounds(10, 32, 54, 24);
		m_pushRadioButton.setText("Push");
		panel.add(m_pushRadioButton);

		m_pollRadioButton = new JRadioButton();
		m_pollRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) 
			{
				m_periodPanel.setEnabled(false);
				m_periodField.setEnabled(false);
				m_periodField.setText("");
				System.out.println("disabled");
			}
		});
		m_pushPollGroup.add(m_pollRadioButton);
		m_pollRadioButton.setText("Poll");
		m_pollRadioButton.setBounds(70, 32, 46, 24);
		panel.add(m_pollRadioButton);

		final JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null), "Visibility", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel_1.setBounds(12, 65, 218, 67);
		frame.getContentPane().add(panel_1);

		m_publicRadioButton = new JRadioButton();
		m_publicRadioButton.setSelected(true);
		m_publicPrivateGroup.add(m_publicRadioButton);
		m_publicRadioButton.setBounds(10, 34, 60, 24);
		m_publicRadioButton.setText("Public");
		panel_1.add(m_publicRadioButton);

		m_privateRadioButton = new JRadioButton();
		m_publicPrivateGroup.add(m_privateRadioButton);
		m_privateRadioButton.setText("Private");
		m_privateRadioButton.setBounds(76, 34, 65, 24);
		panel_1.add(m_privateRadioButton);

		m_periodPanel = new JPanel();
		m_periodPanel.setLayout(null);
		m_periodPanel.setBorder(new TitledBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null), "Update Period", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		m_periodPanel.setBounds(12, 211, 218, 67);
		frame.getContentPane().add(m_periodPanel);

		m_periodField = new JTextField();
		m_periodField.addKeyListener(new KeyAdapter() {
			public void keyReleased(final KeyEvent arg0) 
			{
				String txt = m_periodField.getText();
				try 
				{
					Integer.parseInt(txt);
					
				} 
				catch (NumberFormatException nfe) 
				{
					m_periodField.setText("");
				}
			}
		});
		m_periodField.setBounds(10, 22, 87, 20);
		m_periodPanel.add(m_periodField);

		final JButton okButton = new JButton();
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				
				String type = "";
				switch(m_typeBox.getSelectedIndex())
				{
				case 0:
					type = TemperatureSensor.TYPE;
					break;
				case 1:
					type = HumiditySensor.TYPE;
					break;
				case 2:
					type = AirpressureSensor.TYPE;
					break;
				}
				int period = 0;
				if(!m_pollRadioButton.isSelected())
				{
					try 
					{
						period = Integer.parseInt(m_periodField.getText());
					} catch (Exception e2) {
						JOptionPane.showMessageDialog(frame, "Please enter valid period", "entry check failed", JOptionPane.OK_OPTION);
						return;
					}
					
				}
				
				if(m_mode.equals("create"))
				{
					SensorAttachEvent event = null;
					
					event = new SensorAttachEvent(type,m_nameField.getText(), m_publicRadioButton.isSelected(), m_pollRadioButton.isSelected(),period);

					EventManager.getInstance(m_core).enqueueEvent(event);
				}
				if(m_mode.equals("edit"))
				{
					
					SensorChangedEvent event =new SensorChangedEvent(m_id, type, m_nameField.getText(), m_publicRadioButton.isSelected(), m_pollRadioButton.isSelected(),period);
					EventManager.getInstance(m_core).enqueueEvent(event);
				}
				//Sensor s = new Sensor(m_core,NameService.getInstance(m_core).generateName(),publicRadioButton.isSelected(),pollRadioButton.isSelected());
				System.out.println("Sensor view closed");
				frame.dispose();
			}
		});
		okButton.setText("Ok");
		okButton.setBounds(68, 284, 106, 26);
		frame.getContentPane().add(okButton);

		final JLabel typeLabel = new JLabel();
		typeLabel.setText("Type");
		typeLabel.setBounds(10, 35, 66, 16);
		frame.getContentPane().add(typeLabel);

		m_typeBox = new JComboBox(new Object[]{"Temperature Sensor","Humidity Sensor","Airpressure Sensor"});
		m_typeBox.setBounds(82, 34, 146, 25);
		frame.getContentPane().add(m_typeBox);
	}

}
